import gradio
from langchain_core.prompts import ChatPromptTemplate  
from langchain.prompts import PromptTemplate 
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import HuggingFacePipeline
from langchain_core.prompts.chat import AIMessagePromptTemplate
from langchain.prompts import HumanMessagePromptTemplate 
import time

from ChatGLM_new import zhipu_llm
model  = zhipu_llm 

# model = HuggingFacePipeline.from_model_id(
#     model_id="THUDM/chatglm3-6b",
#     task="text-generation",
#     verbose=True,
#     device=0,
#     model_kwargs={"trust_remote_code":True},
#     pipeline_kwargs={"max_new_tokens": 5000},
# )

prompt = ChatPromptTemplate.from_messages([
        HumanMessagePromptTemplate.from_template("深圳的天气怎么样"),
        AIMessagePromptTemplate.from_template("深圳今天的天气非常好"),
           HumanMessagePromptTemplate.from_template("最近有什么好看的电影"),
        AIMessagePromptTemplate.from_template("非诚勿扰不错"),
           HumanMessagePromptTemplate.from_template("{user_input}"),
            ])

output_parser = StrOutputParser()
chain = prompt | model
def greet(name):
    response = chain.invoke({"user_input": name})
    return response
demo = gradio.Interface(fn=greet, inputs="text", outputs="text")
demo.launch() 